/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.CHoaDon;
import DTO.CFields;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class CHoaDonDAO extends IData<CHoaDon>{
    public CHoaDonDAO(){}

    @Override
    protected Object OnSubmit(CHoaDon item, int t, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected CHoaDon getItem(ResultSet rs) {
        CHoaDon item = new  CHoaDon();
        try {
            item.setId(rs.getLong(CFields.id));
            item.setLoaiPhieu(rs.getString(CFields.loai_phieu));
            item.setNguoiLapId(rs.getLong(CFields.nguoi_lap_id));
            item.setNguoiGiaoId(rs.getLong(CFields.nguoi_giao_id));
            item.setKhachHangId(rs.getLong(CFields.khach_hang_id));
            
        } catch (SQLException ex) {
            Logger.getLogger(CHoaDonDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return item;
    }

    @Override
    public List<CHoaDon> getAll(SubConfig config) {
        String mysql ="select * from " + CFields.tb_hoa_don + " " + this.sWhere;
        return this.getAll(mysql, config);
    }

    @Override
    public Object insertOnSubmit(CHoaDon item, SubConfig config) {
        Object result = 0;
        String mysql = "insert into " + CFields.tb_hoa_don +"("
                + CFields.ngay_nhap + "," + CFields.khach_hang_id + ","
                + CFields.nguoi_lap_id + "," + CFields.nguoi_giao_id + ","
                + CFields.kho_id + "," + CFields.ngay_tao +"," 
                + CFields.loai_phieu +"," + CFields.tinh_trang + ","
                + CFields.tong_tien  + "," + CFields.chiet_khau +","
                + CFields.nguoi_tao
                +") values(?,?,?,?,?,?,?,?,?,?,?)";    
        
        try {
            PreparedStatement cs = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);
            cs.setTimestamp(1, item.getNgayNhap());
            cs.setLong(2, item.getKhachHangId());
            cs.setLong(3, item.getNguoilapId());            
            cs.setLong(4, item.getNguoiGiaoId());            
            cs.setLong(5, item.getKhoId());            
            cs.setTimestamp(6, item.getNgayTao());            
            cs.setString(7, item.getLoaiPhieu());
            cs.setBoolean(8, item.getTinhTrang());
            cs.setDouble(9, item.getTongTien());
            cs.setDouble(10, item.getChietKhau());
            cs.setString(11, item.getNguoiTao());
            cs.executeUpdate();
            ResultSet keyResultSet = cs.getGeneratedKeys();
            if (keyResultSet.next()) {
                result = (long) keyResultSet.getInt(1);      
            }
        } catch (SQLException ex) {
            Logger.getLogger(CHoaDonDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public Object updateOnSubmint(CHoaDon item, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Object deleteOnSubmint(CHoaDon item, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

  

    @Override
    public CHoaDon Find(CHoaDon item, List<CHoaDon> list) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public CHoaDon add(CHoaDon item) {
        try {
            ps.setTimestamp(1, item.getNgayNhap());
            ps.setLong(2, item.getKhachHangId());
            ps.setLong(3, item.getNguoilapId());
            ps.setLong(4, item.getNguoiGiaoId());
            ps.setLong(5, item.getKhoId());
            ps.setTimestamp(6, item.getNgayTao());            
            ps.setString(7, item.getLoaiPhieu());
            ps.setBoolean(8, item.getTinhTrang());
            ps.setDouble(9, item.getTongTien());
            ps.setDouble(10, item.getChietKhau());
            ps.setString(11, item.getNguoiTao());
            ps.addBatch();
        } catch (SQLException ex) {
            Logger.getLogger(CHoaDonDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return item;
    }

    @Override
    public void saveBatch() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void addrows(List<CHoaDon> list, SubConfig config) {
       String mysql = "insert into " + CFields.tb_hoa_don +"("
                + CFields.ngay_nhap + "," + CFields.khach_hang_id + ","
                + CFields.nguoi_lap_id + "," + CFields.nguoi_giao_id + ","
                + CFields.kho_id + "," + CFields.ngay_tao +"," 
                + CFields.loai_phieu +"," + CFields.tinh_trang + ","
                + CFields.tong_tien  + "," + CFields.chiet_khau +","
                + CFields.nguoi_tao
                +") values(?,?,?,?,?,?,?,?,?,?,?)";    
        
        try {
           ps = config.connec.prepareStatement(mysql);
           for(CHoaDon item : list){
                this.add(item);
            }    
           int []n = ps.executeBatch();
           ps.close();
            
        } catch (SQLException ex) {
            Logger.getLogger(CHoaDonDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}
